package tests;

import static org.junit.Assert.*;
import model.filemanager.FileManager;
import model.filemanager.UserQuota;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class UserQuotaTest {
	String user = "test";
	FileManager fm = new FileManager(user);
	UserQuota quota = new UserQuota(user);

	@Before
	public void createFile() {
        helper.setUp();
        addFiles();
	}
	
	private void addFiles() {		
		for (int i = 0; i < 7; i++) {
			String fileName = String.format("%s%d", "image",i);
			String fileKey = String.format("%s%d", "imageKey",i);
			fm.SaveFile(new BlobKey(fileKey), fileName, "image", 100);
		}
		for (int i = 0; i < 5; i++) {
			String fileName = String.format("%s%d", "video",i);
			String fileKey = String.format("%s%d", "videoKey",i);
			fm.SaveFile(new BlobKey(fileKey), fileName, "video", 10000);
		}
		for (int i = 0; i < 6; i++) {
			String fileName = String.format("%s%d", "audio",i);
			String fileKey = String.format("%s%d", "audioKey",i);
			fm.SaveFile(new BlobKey(fileKey), fileName, "audio", 1000);
		}
	}
    private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());


    @After
    public void tearDown() {
    	helper.tearDown();
    }
   
    @Test
    public void testNumberOfImages() {
    	assertEquals(quota.getNumberOfFiles("image"), 7);
    }
    
    @Test
    public void testNumberOfAudios() {
    	assertEquals(quota.getNumberOfFiles("audio"), 6);
    }
    
    @Test
    public void testNumberOfVideos() {
    	assertEquals(quota.getNumberOfFiles("video"), 5);
    }
    
    @Test
    public void testNumberOfAllFiles() {
    	assertEquals(quota.getNumberOfFiles(null), 18);
    }

    @Test
    public void testSizeOfAllImages() {
    	assertEquals(quota.getTotalStorage("image"), 700);
    }
    
    @Test
    public void testSizeOfAllAudios() {
    	assertEquals(quota.getTotalStorage("audio"), 6000);
    }
    
    @Test
    public void testSizeOfAllVideos() {
    	assertEquals(quota.getTotalStorage("video"), 50000);
    }
    
    @Test
    public void testSizeOfAllFiles() {
    	assertEquals(quota.getTotalStorage(null), 700+6000+50000);
    }  
    
    @Test
    public void testNumberOfImagesAfterChanged() {
    	fm.DeleteFile("imageKey0");
    	fm.DeleteFile("audioKey0");
    	fm.DeleteFile("videoKey0");
    	assertEquals(quota.getNumberOfFiles("image"), 6);
    }
    
    @Test
    public void testNumberOfAudiosAfterChanged() {
    	fm.DeleteFile("imageKey0");
    	fm.DeleteFile("audioKey0");
    	fm.DeleteFile("videoKey0");    	
    	assertEquals(quota.getNumberOfFiles("audio"), 5);
    }
    
    @Test
    public void testNumberOfVideosAfterChanged() {
    	fm.DeleteFile("imageKey0");
    	fm.DeleteFile("audioKey0");
    	fm.DeleteFile("videoKey0");    	
    	assertEquals(quota.getNumberOfFiles("video"), 4);
    }
    
    @Test
    public void testNumberOfAllFilesAfterChanged() {
    	fm.DeleteFile("imageKey0");
    	fm.DeleteFile("audioKey0");
    	fm.DeleteFile("videoKey0");    	
    	assertEquals(quota.getNumberOfFiles(null), 15);
    }    
}
